<template>
	<view class="shop-cart" :style="'bottom: '+bottom+'px;'">
		<view style="margin-left: 20px;position: relative;flex-grow: 0;" @click="toCart">
			<u-icon name="shopping-cart-fill" size="40"></u-icon>
			<view style="position: absolute;left: 23px;top: 0">
				<u-badge  :value="allNum"></u-badge>
			</view>
		</view>
		<view style="flex-grow: 1;margin-left: 20px;display: flex;flex-direction: column;" @click="toCart">
			<view>
				<text :style="'color: '+app_color+';font-weight: 600;font-size: 16px;'">￥{{shopPrice}}</text>
				<text style="font-size: 12px;color: #999;text-decoration: line-through;margin-left: 5px;;">￥{{marketPrice}}</text>
			</view>
			<text style="font-size: 11px;color: #999;">(不含配送)</text>
		</view>
		<view class="tobuy" @click="createdOrder"
		 :style="'background: linear-gradient(to right,'+app_color+','+app_color_t+');'">
			<text>去结算</text>
		</view>
	</view>
</template>

<script>
	export default {
		name:"moxi-shop-cart",
		data() {
			return {
				cartList:[],
				app_color:"",
				app_color_t:"",
				hasTabbar:false,
				allNum:0,//购物车总数量
				shopPrice:0,
				marketPrice:0,
				bottom:15
			};
		},created() {
			this.app_color = this.$appData.data.app_color;
			this.app_color_t = uni.$u.colorToRgba(this.app_color, 0.7);
			if(this.$appData.hasTabbar){
				this.bottom = 65;
			}
		},methods:{
			createdOrder(){
				let param = [];
				for(let k in this.cartList){
					for(let s in this.cartList[k].spec_info){
					
						param.push({
							goods_id:this.cartList[k].goods_id,
							spec_name:s,
							num:this.cartList[k].spec_info[s].num
						})
						
					}
				}
				
				let self = this;
				this.moxiPost('/plugin/mall/user/order/add',{goods:param},function(data){
					if(data.data){
						self.navto('page_mall/order/created_order?is_cart=1&order_id='+data.data);
					}
				})
			},
			toCart(){
				this.navto('page_mall/goods/shopcart')
			},
			init(){
				let self = this;
				this.moxiPost("/plugin/mall/user/cart/get",{},function(data){
					self.cartList = data.data;
					self.$emit('change',data.data)
					self.allNum = 0;
					self.shopPrice = 0;
					self.marketPrice = 0;
					for(let k in self.cartList){
						for(let s in self.cartList[k].spec_info){
							self.allNum = self.allNum+self.cartList[k].spec_info[s].num;
							self.shopPrice = Number(self.cartList[k].spec_info[s].shop_price)*self.cartList[k].spec_info[s].num+self.shopPrice;
							self.marketPrice = Number(self.cartList[k].spec_info[s].market_price)*self.cartList[k].spec_info[s].num+self.marketPrice;
						}
					}
				},false)
			}
		}
	}
</script>

<style>
.shop-cart{width: 700rpx;height: 45px;border-radius:100px;z-index: 100;display: flex;flex-direction: row;
justify-content: space-between;align-items: center;
box-shadow: 0 0 10px 0 rgba(0,0,0,.5);
background: rgba(255,255,255,.9);position: fixed;left: 25rpx;overflow-y: hidden;}
.tobuy{display: flex;flex-direction: row;align-items: center;background-color: red;height: 100%;
padding:0 20px;flex-grow: 0;
font-size: 15px;color: #fff}
</style>